import {ClassicModel} from '../../models/classic.js'
import {LikeModel} from '../../models/like.js'

let classicModel = new ClassicModel()
let likeMode = new LikeModel()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    data:null,
    first: false,
    latest: true,
    likeCount: 0,
    likeStatus: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    classicModel.getLatest((res) => {
      this.setData({
        data: res,
        likeCount: res.fav_nums,
        likeStatus: res.like_status
      })
    })
  },

  onLike: function(event) {
    let behavior = event.detail.behavior
    likeMode.like(behavior, this.data.data.id, this.data.data.type)
  },

  onLeft() {
    this._getEpsoide('next')
  },

  onRight() {
    this._getEpsoide('previous')
  },

  _getEpsoide(command) {
    let index = this.data.data.index
    classicModel.onGetEpsoide(index, command, (res) => {
      this._getClassicLikeStatus(res.id, res.type)
      this.setData({
        data:res,
        first: classicModel.isFirst(res.index),
        latest: classicModel.isLatest(res.index)
      })
    })
  },

  _getClassicLikeStatus(artId, type) {
    likeMode._getLikeStatus(artId, type, (res) => {
      this.setData({
        likeCount: res.fav_nums,
        likeStatus: res.like_status
      })
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})